package com.jt.controller;

import com.fasterxml.jackson.databind.util.JSONPObject;
import com.jt.Service.UserService;
import com.jt.pojo.User;
import com.jt.vo.SysResult;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisCluster;


import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.CollationKey;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private JedisCluster jedisCluster;

    @RequestMapping("/findAll")
    public List<User> findAll() {
        return userService.findAll();
    }

    /**
     * 需求分析：检验用户的数据是否可用
     * url:http://sso.jt.com/user/check/{param}/{tyoe}
     * 参数：param、type
     * 返回值结果：SysResult对象
     */
    @RequestMapping("/check/{param}/{type}")
    public JSONPObject checkUser(@PathVariable String param, @PathVariable Integer type, String callback) {
        //根据信息查询数据库获取响应的记录
        Boolean flag = userService.checkUser(param, type);
        return new JSONPObject(callback, SysResult.success(flag));

    }

    /**
     * 测试httpClient调用方式
     * url地址：
     * 返回：user对象的JSON字符串
     */
    @RequestMapping("/findUserById/{userId}")
    public User findUserById(@PathVariable Long userId) {
        return userService.findUserById(userId);
    }

    /**
     * 根据ticket信息查询用户的json信息jsonp请求，返回值使用特定的对象封装
     */
//    @RequestMapping("/query/{ticket}")
//    public JSONPObject findUserByTicket(@PathVariable String ticket, HttpServletResponse response,String callback){
//        if(jedisCluster.exists(ticket)){
//            //可以正确返回
//            String userJSON = jedisCluster.get(ticket);
//            return  new JSONPObject(callback,SysResult.success(userJSON));
//        }else{
//            //如果根据ticket查询有误，则应该删除Cookie信息
//            Cookie cookie = new Cookie("JT_TICKET","");
//            cookie.setDomain("jt.com");
//            cookie.setPath("/");
//            cookie.setMaxAge(0);
//            response.addCookie(cookie);
//            return new JSONPObject(callback,SysResult.fail());
//        }
//    }
    @RequestMapping("/query/{ticket}")
    public JSONPObject findUserByTicket(@PathVariable String ticket, HttpServletResponse response,
                                        String callback) {
        if (jedisCluster.exists(ticket)) {
            //可以正确返回
            String userJSON = jedisCluster.get(ticket);
            return new JSONPObject(callback, SysResult.success(userJSON));
        } else {
            //如果根据ticket查询有误,则应该删除Cookie信息.
            Cookie cookie = new Cookie("JT_TICKET", "");
            cookie.setDomain("jt.com");
            cookie.setPath("/");
            cookie.setMaxAge(0);
            response.addCookie(cookie);
            return new JSONPObject(callback, SysResult.fail());
        }

    }


}

